package com.petcare;

import java.util.ArrayList;
import java.util.List;

import com.bd.PetsModule;
import com.bd.VetsModule;
import com.bd.Utilities;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

public class cambiarvet extends Activity {

	VetsModule myvet = null;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_cambiarvet);
	    
	    try{
            Bundle bundle = this.getIntent().getExtras();
    	    myvet = Utilities.datasource.getVet(bundle.getInt("pk")+"");
    	    System.out.println(myvet);
    	    }
            catch(Exception e){
            	System.out.println("Exception: " +e);
           }
	    
	    final EditText name = (EditText) findViewById(R.id.editText1);
	    name.setText(myvet.getVetname());
	    final EditText phone = (EditText) findViewById(R.id.editText2);
	    phone.setText(myvet.getVetphone());
	    final EditText address = (EditText) findViewById(R.id.editText3);
	    address.setText(myvet.getVetaddr());
        
	    final List<PetsModule> list = Utilities.datasource.getAllPets();
	    List<String> SpinnerArray =  new ArrayList<String>();
	    
	    for(int i = 0; i < list.size(); i++){
	    	
	    	final PetsModule pet = list.get(i);
	    	String nombre = pet.getName();
	    	SpinnerArray.add(nombre);
	    }
	    	
	    
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    final Spinner Items = (Spinner) findViewById(R.id.spinner2);
	    Items.setAdapter(adapter);
	    Items.setSelection(Integer.parseInt(myvet.getPetid())-1);
        
        //Guardar
        Button bsave = (Button) findViewById(R.id.button3);
        bsave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String petID = String.valueOf(list.get(Items.getSelectedItemPosition()).getPk());
				String nombre = name.getText().toString();
				String tel = phone.getText().toString();
				String dir = address.getText().toString();
				myvet.setPetid(petID);
				myvet.setVetaddr(dir);
				myvet.setVetname(nombre);
				myvet.setVetphone(tel);
				Utilities.datasource.updateVet(myvet);
				Toast.makeText(getBaseContext(), "Vet updated!", Toast.LENGTH_SHORT).show();
			}
			
		}
        );
        

        
	    
	    final Button button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View v) {
            	Intent myIntent = new Intent(cambiarvet.this, help.class);
            	startActivity(myIntent);
            }
        });
        
        final Button button6 = (Button) findViewById(R.id.button6);
        button6.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View v) {
            	Intent myIntent = new Intent(cambiarvet.this, mainMenu.class);
            	startActivity(myIntent);
            }
        });
	
        
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
	
	

}
